using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using EntityLayer;
using BusinessLayer;
using InfraStructureLayer;
using WebControls;

public partial class Admin_Pages : SFPage
{
    entPages insEntPages = new entPages();
    busPages insBusPages = new busPages();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected bool sfgPages_NeedsDataSource()
    {
        sfgPages.SetData(CurrentContext.Pages);
        return false;
    }
    protected void btnNew_Click(object sender, EventArgs e)
    {
        this.Id = 0;
        this.OpenEditPanel();
    }
    protected void sfgPages_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditData")
        {
            this.Id = Convert.ToInt32(e.CommandArgument);
            this.OpenEditPanel();
        }
        else if (e.CommandName == "DeleteData")
        {
            insEntPages.PageId = Convert.ToInt32(e.CommandArgument);
            insBusPages.DeletePagesByPageId(insEntPages);
            if (insBusPages.HasErrors)
            {
                MessageBoxShow(insBusPages);
                return;
            }
            CurrentContext.Pages = null;
            this.LoadPages();
            sfgPages.LoadGridData();
            this.CloseEditPanel();
        }
    }
    public override void OpenEditPanel()
    {
        this.Clear(this.Master.FindControl("ContentPlaceHolderEdit"));
        base.OpenEditPanel();
        FillEditPanel();
    }
    private void FillEditPanel()
    {
        if (this.Id!=0)
        {
            insEntPages.PageId = this.Id;
            insBusPages.SelectPagesByPageId(insEntPages);
            txtPageId.Text = insEntPages.PageId.ToString();
            txtPageName.Text = insEntPages.PageClassName;
            txtPageDescription.Text = insEntPages.PageDescription;
            chkIsAdminPage.Checked = insEntPages.IsAdminPage;
        }
        else
        {
            txtPageId.Text = "";
            txtPageName.Text = "";
            txtPageDescription.Text = "";
            chkIsAdminPage.Checked = false;
        }
    }

    public override void Save()
    {
        this.Validate();
        if (this.HasErrors)
        {
            MessageBoxShow(this);
            return;
        }
        insEntPages.PageId= txtPageId.Value.Value;
        insEntPages.PageClassName = txtPageName.Text;
        insEntPages.PageDescription = txtPageDescription.Text;
        insEntPages.IsAdminPage = chkIsAdminPage.Checked;
        base.Save();
    }
    public override void SaveNew()
    {
        insBusPages.InsertPages(insEntPages);
        if (insBusPages.HasErrors)
        {
            MessageBoxShow(insBusPages);
            return;
        }
        base.SaveNew();
        CurrentContext.Pages = null;
        this.LoadPages();
        sfgPages.LoadGridData();
    }
    public override void SaveExisting()
    {
        insEntPages.PageId = this.Id;
        insBusPages.UpdatePagesByPageId(insEntPages);
        if (insBusPages.HasErrors)
        {
            MessageBoxShow(insBusPages);
            return;
        }
        base.SaveExisting();
        
        CurrentContext.Pages = null;
        this.LoadPages();
        sfgPages.LoadGridData();
    }
}
